/*
 * PreferencesDialog.java
 *
 * Created on November 19, 2005, 7:34 PM
 */
package ika.gui;

import java.util.prefs.*;
import javax.swing.*;
import java.awt.*;

/**
 *
 * @author  jenny
 */
public class PreferencesDialog extends javax.swing.JPanel {

    public static String SHOW_ADVANCED_TRANSFORMATION_INFO = "SHOW_ADVANCED_TRANSFORMATION_INFO";
    public static String SHOW_MEKENKAMP = "SHOW_MEKENKAMP";

    public static Preferences getPreferences() {
        return Preferences.userNodeForPackage(ika.gui.PreferencesDialog.class);
    }

    public static String getPreference(String name, String defaultValue) {
        Preferences prefs =
                Preferences.userNodeForPackage(ika.gui.PreferencesDialog.class);
        return prefs.get(name, defaultValue);
    }

    static public void showPreferencesDialog() {
        Frame parentFrame = null;
        try {
            // find window that currently has focus.
            // null is returned if none of the windows has the focus.
            Window windowFocusOwner =
                    KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
            parentFrame = (Frame) windowFocusOwner;
            if (MainWindow.windows.isEmpty()) {
                parentFrame = null;
            }
        } catch (Exception e) {
        }

        PreferencesDialog dialog = new PreferencesDialog();

        // initialize values of components
        Preferences prefs = PreferencesDialog.getPreferences();
        String res = prefs.get(SHOW_MEKENKAMP, Boolean.toString(false));
        boolean showMekenkamp = Boolean.valueOf(res).booleanValue();
        dialog.mekenkampCheckBox.setSelected(showMekenkamp);

        res = prefs.get(SHOW_ADVANCED_TRANSFORMATION_INFO, Boolean.toString(false));
        boolean showAdvancedTrafoInfo = Boolean.valueOf(res).booleanValue();
        dialog.advancedTransInfoCheckBox.setSelected(showAdvancedTrafoInfo);

        String title = "MapAnalyst Preferences";
        Object[] options = {"OK", "Cancel"};
        int resID = JOptionPane.showOptionDialog(parentFrame,
                dialog,
                title,
                JOptionPane.YES_NO_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                options,
                options[0]);
        if (resID == JOptionPane.CANCEL_OPTION || resID == JOptionPane.CLOSED_OPTION || resID == 1) {
            return;
        }

        // read settings from dialog
        showAdvancedTrafoInfo = dialog.advancedTransInfoCheckBox.isSelected();
        showMekenkamp = dialog.mekenkampCheckBox.isSelected();

        // first write new settings to preferences
        prefs.put(SHOW_MEKENKAMP, Boolean.toString(showMekenkamp));
        prefs.put(SHOW_ADVANCED_TRANSFORMATION_INFO,
                Boolean.toString(showAdvancedTrafoInfo));

        // then inform all windows about the changes
        final int nbrWindows = MainWindow.windows.size();
        for (int i = nbrWindows - 1; i >= 0; i--) {
            MainWindow w = (MainWindow) MainWindow.windows.get(i);
            w.synchronizeWithPreferences();
        }
    }

    /** Creates new form PreferencesDialog */
    public PreferencesDialog() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        mekenkampCheckBox = new javax.swing.JCheckBox();
        advancedTransInfoCheckBox = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        mekenkampCheckBox.setText("Enable Standard Inaccuracy Circles After Mekenkamp");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 15);
        add(mekenkampCheckBox, gridBagConstraints);

        advancedTransInfoCheckBox.setText("Show Advanced Information for Transformation");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 0, 15);
        add(advancedTransInfoCheckBox, gridBagConstraints);

        jLabel1.setText("<html><small>Mekenkamp circles are experimental and not recommended for most applications.</small></html>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 44, 15, 15);
        add(jLabel1, gridBagConstraints);

        jLabel2.setText("<html><small>Additional information about the transformation is displayed at the bottom right of the main window.</small></html>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 44, 15, 15);
        add(jLabel2, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JCheckBox advancedTransInfoCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    public javax.swing.JCheckBox mekenkampCheckBox;
    // End of variables declaration//GEN-END:variables
}
